﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class Pages_BusView : System.Web.UI.Page
{
	protected void Page_Load(object sender, EventArgs e)
	{
		if (!IsPostBack)
		{
			gvBuses.Visible = false;
			lblResult.Text = string.Empty;
		}
	}

	protected void tbBusNames_TextChanged(object sender, EventArgs e)
	{
		gvBuses.Visible = true;
		RefreshGrid();
	}

	protected void RefreshGrid()
	{
		lblResult.Text = string.Empty;

		string busName = tbBusName.Text.Trim();
		if (!String.IsNullOrEmpty(busName))
		{
			Bus tmpBus = new Bus();
			tmpBus.Name = busName;
			IList<Bus> buses = DBEntity<Bus>.Select(tmpBus);
			gvBuses.DataSource = buses;
			gvBuses.DataBind();
		}
	}

	protected void btnDelete_Command(object sender, CommandEventArgs e)
	{
		try
		{
			DBEntity<Bus>.Delete(DS.ToInt64(e.CommandArgument));
			RefreshGrid();
		}
		catch
		{
			lblResult.Text = "Не удалось удалить автобус. Вероятно, с ним связаны заданные в системе путешествия.";
			return;
		}
	}

}